package com.focusai.efairy.business.manager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;


public class ThreadPoolManager {
    private static ExecutorService mCachedThreadPool;
    private static ExecutorService mSingleThreadExecutor;
    private static ExecutorService mSinglePosThreadExecutor;
    private static ScheduledExecutorService mScheduledExecutor;
    private static final int DEFAULT_SCHEDULED_THREAD_NUM = 5;

    /**
     * 获取缓存型线程池单例对象（第一种）
     * @return
     */
    public static ExecutorService getCachedThreadPool() {
        if(mCachedThreadPool == null) {
            synchronized (ThreadPoolManager.class) {
                if(mCachedThreadPool == null) {
                    mCachedThreadPool = Executors.newCachedThreadPool();
                }
            }
        }
        return mCachedThreadPool;
    }

    /**
     * 将任务放到线程池中执行
     * @param runnable
     * @return
     */
    public static Future<?> submitInCachedThreadPool(Runnable runnable){
        if(runnable != null) {
            return getCachedThreadPool().submit(runnable);
        }
        return null;
    }

    /**
     * 将任务放到线程池中执行
     * @param runnable
     */
    public static void executeInCachedThreadPool(Runnable runnable){
        if(runnable != null) {
            getCachedThreadPool().execute(runnable);
        }
    }

    /**
     * 获取单线程池单例对象（第二种）
     * @return
     */
    public static ExecutorService getSingleThreadPool() {
        if(mSingleThreadExecutor == null) {
            synchronized (ThreadPoolManager.class) {
                if(mSingleThreadExecutor == null) {
                    mSingleThreadExecutor = Executors.newSingleThreadExecutor();
                }
            }
        }
        return mSingleThreadExecutor;
    }

    /**
     * 将任务放到线程池中执行
     * @param runnable
     * @return
     */
    public static Future<?> submitInSingleThreadPool(Runnable runnable){
        if(runnable != null) {
            return getSingleThreadPool().submit(runnable);
        }
        return null;
    }

    /**
     * 将任务放到线程池中执行
     * @param runnable
     */
    public static void executeInSingleThreadPool(Runnable runnable){
        if(runnable != null) {
            getSingleThreadPool().execute(runnable);
        }
    }

    /**
     * 获取单线程池单例对象（第二种，用于POS服务）
     * @return
     */
    public static ExecutorService getSinglePosThreadPool() {
        if(mSinglePosThreadExecutor == null) {
            synchronized (ThreadPoolManager.class) {
                if(mSinglePosThreadExecutor == null) {
                    mSinglePosThreadExecutor = Executors.newSingleThreadExecutor();
                }
            }
        }
        return mSinglePosThreadExecutor;
    }

    /**
     * 将任务放到线程池中执行
     * @param runnable
     * @return
     */
    public static Future<?> submitInSinglePosThreadPool(Runnable runnable){
        if(runnable != null) {
            return getSinglePosThreadPool().submit(runnable);
        }
        return null;
    }

    /**
     * 将任务放到线程池中执行
     * @param runnable
     */
    public static void executeInSinglePosThreadPool(Runnable runnable){
        if(runnable != null) {
            getSinglePosThreadPool().execute(runnable);
        }
    }

    /**
     * 获取日程单线程池单例对象（第三种）
     * @return
     */
    public static ScheduledExecutorService getScheduledThreadPool() {
        if(mScheduledExecutor == null) {
            synchronized (ThreadPoolManager.class) {
                if(mScheduledExecutor == null) {
                    mScheduledExecutor = Executors.newScheduledThreadPool(DEFAULT_SCHEDULED_THREAD_NUM);
                }
            }
        }
        return mScheduledExecutor;
    }
}